package com.mach.platform.service;


import com.google.common.collect.Lists;
import com.mach.platform.domain.ProductionOrder;
import com.mach.platform.domain.ProductionOrderTerm;
import com.mach.platform.repository.ProductionOrderRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class ProductionOrderServiceImpl implements BaseService<ProductionOrder> {
    @Autowired
    private ProductionOrderRepo productionOrderRepo;
    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public ProductionOrder save(ProductionOrder productionOrder) {
        if (!ObjectUtils.allNotNull(productionOrder.getTime())) {
            productionOrder.setTime(LocalDateTime.now());
        }
        return productionOrderRepo.save(productionOrder);
    }

    @Override
    public List<ProductionOrder> insertObjInBatch(List<ProductionOrder> list) {
        return null;
    }

    @Override
    public ProductionOrder update(ProductionOrder productionOrder) {
        return productionOrderRepo.save(productionOrder);
    }

    @Override
    public int delete(Long[] ids) {
        return 0;
    }

    @Override
    public Page<ProductionOrder> list(Pageable pageable, Map params) {

        String keyword = null;
        String time = null;
        if (params.size() > 0){
            Object keywordObject = params.get("keyword");
            Object timeObject = params.get("time");
            if (ObjectUtils.allNotNull(keywordObject)){
                keyword = keywordObject.toString();
            }
            if (ObjectUtils.allNotNull(timeObject)){
                time = timeObject.toString();
                time = time.substring(0,10);
            }
            System.out.println(time);
//            Object startTime = params.get("startTime");
//            Object endTime = params.get("endTime");
//            if (ObjectUtils.allNotNull(keyword)) {
//                params.put("classBan_plike", keyword.toString().trim());
//                params.remove("keyword");
//            }
            /*if (startTime != null && startTime != "") {
                params.put("time_gte", LocalDateTime.parse(startTime.toString().trim(), df));
                params.remove("startTime");
            }
            if (endTime != null && endTime != "") {
                params.put("time_lte", LocalDateTime.parse(endTime.toString().trim(), df));
                params.remove("endTime");
            }*/
        }
        //Specification specification = DynamicSpecifications.bySearchFilter(SearchFilter.parse(params).values(), ProductionOrder.class);
        //Page<ProductionOrder> result = productionOrderRepo.findAll(specification, pageable);


        Page<ProductionOrder> result = productionOrderRepo.getByTimeAndClassBan(time, keyword, pageable);

        return result;
    }

    /**
     * 获取生产单
     * @param pageable
     * @param params
     * @return
     */
    public List<ProductionOrder> getProductionOrder(Pageable pageable, Map params){
        List<ProductionOrder> productionOrders = Lists.newArrayList();
        Object time = null;
        if (params.size() > 0){
            time = params.get("time");
            params.remove("time");
        }
        Page<ProductionOrder> result = this.list(pageable, params);
        if (time != null && !time.equals("")){
            if (result.getSize() > 0){
                for (ProductionOrder productionOrder : result){
                    LocalDateTime localDateTime = productionOrder.getTime();
                    if (localDateTime != null){
                        if (time.toString().substring(0, 7).equals(localDateTime.format(df))){
                            productionOrders.add(productionOrder);
                        }
                    }
                }
            }
        }
        return productionOrders;
    }

    @Override
    public ProductionOrder info(Long id) {
        return null;
    }


    public List<ProductionOrder> getProductionOrder(String time, String classBan) {
        time = time.substring(0,10);
        List<ProductionOrder> productionOrders = productionOrderRepo.getDataByTimeAndClassBan(time, classBan);
        return productionOrders;
    }
}
